﻿namespace ClassyBlog.Infrastructure.Specs.Unit
{
    using System;
    using System.Web.Routing;

    using Machine.Specifications;

    [Subject(typeof(PositiveIntegerConstraint))]
    public class when_matching_positive_integer_with_null_values
    {
        static PositiveIntegerConstraint constraint;
        static Exception exception;

        Establish context = () =>
            constraint = new PositiveIntegerConstraint();

        Because of = () => exception =
            Catch.Exception(() =>
                constraint.Match(
                null,
                null,
                "pages",
                null,
                RouteDirection.IncomingRequest));

        It should_throw_exception = () =>
            exception.ShouldBeOfType<ArgumentNullException>();
    }

    [Subject(typeof(PositiveIntegerConstraint))]
    public class when_integer_value_is_found
    {
        static PositiveIntegerConstraint constraint;
        static bool matched;

        Establish context = () =>
            constraint = new PositiveIntegerConstraint();

        Because of = () =>
            matched = constraint.Match(
            null,
            null,
            "page",
            new RouteValueDictionary
                {
                    { "page", 5 }
                },
            RouteDirection.IncomingRequest);

        It should_match = () => matched.ShouldBeTrue();
    }

    [Subject(typeof(PositiveIntegerConstraint))]
    public class when_optional_and_value_is_found
    {
        static PositiveIntegerConstraint constraint;
        static bool matched;

        Establish context = () =>
            constraint = new PositiveIntegerConstraint(true);

        Because of = () =>
            matched = constraint.Match(
            null,
            null,
            "page",
            new RouteValueDictionary(),
            RouteDirection.IncomingRequest);

        It should_match = () => matched.ShouldBeTrue();
    }

    [Subject(typeof(PositiveIntegerConstraint))]
    public class when_required_and_no_value_found
    {
        static PositiveIntegerConstraint constraint;
        static bool matched;

        Establish context = () =>
            constraint = new PositiveIntegerConstraint();

        Because of = () =>
            matched = constraint.Match(
            null,
            null,
            "page",
            new RouteValueDictionary(),
            RouteDirection.IncomingRequest);

        It should_not_match = () => matched.ShouldBeFalse();
    }

    [Subject(typeof(PositiveIntegerConstraint))]
    public class when_required_and_no_integer_value_found
    {
        static PositiveIntegerConstraint constraint;
        static bool matched;

        Establish context = () =>
            constraint = new PositiveIntegerConstraint();

        Because of = () =>
            matched = constraint.Match(
            null,
            null,
            "page",
            new RouteValueDictionary
                {
                    { "page", "foobar" }
                },
            RouteDirection.IncomingRequest);

        It should_not_match = () => matched.ShouldBeFalse();
    }
}